﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DeepTalkDBServiceTest.DTDBService;
using System.IO;
using IndicoInterface;

namespace DeepTalkDBServiceTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class t_UnrenderedTalks
    {
        public t_UnrenderedTalks()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestInitialize]
        public void InitTest()
        {
            FileInfo db = new FileInfo("ConfData_" + TestContext.TestName + ".sdf");
            File.Copy(@"..\..\..\DeepTalkDBService\ConferenceData.sdf", db.FullName);
            using (DTDBClient rc = new DTDBClient())
            {
                rc.SetDBFile(db);
            }
        }

        [TestMethod]
        public void TestCreate()
        {
            /// Can we create/destory the service?

            using (DTDBService.DTDBClient dc = new DeepTalkDBServiceTest.DTDBService.DTDBClient())
            {
            }
        }

        [TestMethod]
        public void TestEmptyDBUnrendered()
        {
            /// Ask a new, empty DB, for some talks that aren't rendered yet. Make sure it comes back empty!

            using (DTDBService.DTDBClient rc = new DeepTalkDBServiceTest.DTDBService.DTDBClient())
            {
                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 0, "Came back with more than zero talks!");
            }
        }

        [TestMethod]
        public void TestOneTalkInsert()
        {
            using (DTDBClient rc = new DTDBClient())
            {
                DateTime talkTime = DateTime.Now;
                string talkTitle = "Talk 1";
                string talkXML = GetTalkXML("123");
                rc.SubmitNewTalk(talkXML, talkTitle, talkTime);

                /// The DB time is truncated -- doesn't have full resolution that DateTime does - so pause a second
                /// to make sure that "time passes" and we aren't getting fool by the times.

                System.Threading.Thread.Sleep(1000);

                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 1, "Should be only one talk");

                BasicTalkInfo talk = talks[0];
                TimeSpan span = talk.StartDate - talkTime;
                Assert.IsTrue(span.Milliseconds < 10, "The talk time and span aren't right!");
                Assert.AreEqual(talkTitle, talk.Title);
            }
        }

        [TestMethod]
        public void TestTwoTalkInsert()
        {
            using (DTDBClient rc = new DTDBClient())
            {
                rc.SubmitNewTalk(GetTalkXML("123"), "Talk 1", DateTime.Now);
                rc.SubmitNewTalk(GetTalkXML("234"), "Talk 2", DateTime.Now);

                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 2, "Should have gotten back both inserted talks");
            }
        }

        [TestMethod]
        public void TestInsertTalkInOldDB()
        {
            using (DTDBClient rc = new DTDBClient())
            {
                FileInfo db = new FileInfo("ConfData_OldWithTalks.sdf");
                File.Copy(@"..\..\..\DeepTalkDBServiceTest\ConferenceDataWithTwoTalks.sdf", db.FullName);
                rc.SetDBFile(db);

                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 2, "Initial old DB should have two talks in it!");

                rc.SubmitNewTalk(GetTalkXML("12372731a"), "Talk 3", DateTime.Now);
                talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 3, "After new talk we should have three talks!");
            }
            using (DTDBClient rc = new DTDBClient())
            {
            }
        }

        [TestMethod]
        public void TestLimitedReturn()
        {
            /// Insert 3 talks, ask for 2 back, make sure we get the right ones back too

            using (DTDBService.DTDBClient rc = new DeepTalkDBServiceTest.DTDBService.DTDBClient())
            {
                rc.SubmitNewTalk(GetTalkXML("123"), "Talk 1", DateTime.Now);
                rc.SubmitNewTalk(GetTalkXML("124"), "Talk 2", DateTime.Now);
                rc.SubmitNewTalk(GetTalkXML("125"), "Talk 3", DateTime.Now);
                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(2);
                Assert.IsTrue(talks.Length == 2, "Should have gotten only two talks back!");
            }
        }

        [TestMethod]
        public void TestTalkReallyIsntRendered()
        {
            /// Insert 3 talks, ask for 2 back, make sure we get the right ones back too

            using (DTDBService.DTDBClient rc = new DeepTalkDBServiceTest.DTDBService.DTDBClient())
            {
                string agendaXML = GetTalkXML("123");
                rc.SubmitNewTalk(agendaXML, "Talk 1", DateTime.Now);
                int id = rc.FindTalk(agendaXML);
                Assert.IsFalse(rc.CanBeViewed(id), "Talk should not be viewable right now!");
            }
        }

        [TestMethod]
        public void TestTalkOrderForward()
        {
            /// Insert three talks, make sure that the "order" they come back in is correct
            using (DTDBService.DTDBClient rc = new DeepTalkDBServiceTest.DTDBService.DTDBClient())
            {
                rc.SubmitNewTalk(GetTalkXML("123"), "Talk 1", DateTime.Now);
                rc.SubmitNewTalk(GetTalkXML("124"), "Talk 2", DateTime.Now);
                rc.SubmitNewTalk(GetTalkXML("125"), "Talk 3", DateTime.Now);
                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(2);
                string bogus = talks[0].ToString();

                Assert.AreEqual("Talk 1", talks[0].Title, "Talk 1 should be first");
                Assert.AreEqual("Talk 2", talks[1].Title, "Talk 2 should be second");
            }
        }

        string GetTalkXML (string id)
        {
            /// Submit a talk for rendering -- should not crash! :-)

            AgendaInfo info = new AgendaInfo("http://indico.cern.ch/conferenceDisplay.py?showSession=all&showDate=all&view=xml&fr=no&confId=" + id);
            StringWriter sr = new StringWriter();
            info.Seralize(sr);
            return sr.ToString();
        }

        string GetTalkXML()
        {
            return GetTalkXML ("a042880");
        }

        [TestMethod]
        public void TestRenderRequest()
        {
            using (DTDBClient rc = new DTDBClient())
            {
                rc.SubmitNewTalk(GetTalkXML(), "This is a Title", DateTime.Now);
                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 1, "submitted talk was not found!");
            }
        }

        [TestMethod]
        public void TestRepeatRenderRequest()
        {
            /// Submit a talk twice -- should have no effect

            using (DTDBClient rc = new DTDBClient())
            {
                string xml = GetTalkXML();
                rc.SubmitNewTalk(xml, "This is a title", DateTime.Now);
                rc.SubmitNewTalk(xml, "This is a title", DateTime.Now);

                BasicTalkInfo[] talks = rc.GetUnrenderedTalks(10);
                Assert.IsTrue(talks.Length == 1, "Should only be one talk when same talk is re-submitted");
            }
        }
    }
}
